﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FONCREAGROINTEGRAL.Models;
using System.Globalization;
namespace FONCREAGROINTEGRAL.Controllers
{
    public class limitesrquintaController : Controller
    {
        public SistIntFoncreagroEntities entidades = new SistIntFoncreagroEntities();

        public FONCREAGROINTEGRAL.Models.LIMITESQUINTA LIMITESQUINTA
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }
        public ActionResult Index()
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);

            string apnombres = Convert.ToString(Request.QueryString["query"]);
            var filtrer = Convert.ToString(Request.QueryString["filter"]);


            if (filtrer != null)
            {
                string parametro = filtrer.Remove(0, 30);
                char[] MyChar = { '\\', '"', '}', ']', '[', '}' };
                parametro = parametro.Trim(MyChar);
                apnombres = parametro;
            }

            string limites = Convert.ToString(Request.QueryString["query"]);

            var query = entidades.LIMITESQUINTAs
                .Where(a => a.idlimitequinta >= 1);
/*            if (!String.IsNullOrEmpty(limites))
            {
                query = query.Where(a => (a.).ToUpper().Contains(cargo.ToUpper()));
            }
  
 */
            var result = (from a in query.OrderBy(a => a.porcentaje).Skip(start).Take(limit).ToList()

                          join n in entidades.EJERCICIOs on a.idejercicio equals n.IdEjercicio
                          where n.EstadoP == true 
                          select new
                          {
                              Idlimitequinta = a.idlimitequinta,
                              Inferior = a.inferior,
                              Superior = a.superior,
                              Porcentaje= a.porcentaje,
                              Cardinal= a.cardinal,
                              Estado = a.estado

                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
   
        }

        public ActionResult IndexLimiteQuinta(int id)
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);

            var query = entidades.LIMITESQUINTAs
                .Where(a => a.idejercicio == id);

            var result = (from a in query.OrderBy(a => a.porcentaje).Skip(start).Take(limit).ToList()
                          select new
                          {
                              Idlimitequinta = a.idlimitequinta,
                              Inferior = a.inferior,
                              Superior = a.superior,
                              Porcentaje = a.porcentaje,
                              Cardinal = a.cardinal,
                              Estado = a.estado

                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);

        }

        public ActionResult Create()
        {
            return View();
        }



        [HttpPost]
        public ActionResult Create(FormCollection collection, LIMITESQUINTA limitesquinta)
        {
            int idpersonal = Convert.ToInt32(Request.Params["IdlimiteRentaQuinta"]);

            var url = Request.RawUrl;
            var uri = Request.Url;

            var inferior = Request.Params["inferior"];
            var cardinal =  Request.Params["cardinal"];
            var superior =Request.Params["superior"];
            var porcentaje =Request.Params["porcentaje"];

            var autorizador = Request.Params["iddeduccion"];
            var autorizador2 = Request.Params["idejercicio"];
            var autorizador3 = Request.Params["estado"];

            var a = Request.Params["iddeduccion"];
            var b = Request.Params["idejercicio"];
            var estado = Request.Params["estado"];

            try
            {
                if (ModelState.IsValid)
                {

                    var lrquinta = new LIMITESQUINTA();
                   // lrquinta.cardinal = cardinal;
                    //lrquinta.inferior = inferior;
                    //lrquinta.superior = superior;
                    //lrquinta.porcentaje = porcentaje;

                    TryUpdateModel(lrquinta, collection.ToValueProvider());
                    entidades.AddToLIMITESQUINTAs(lrquinta);
                    entidades.SaveChanges();
                    return Content("OK");
                }
                return View();
            }
            catch
            {
                return View();
            }
        }


        [HttpPost]
        public ActionResult Manual(int id, FormCollection collection)
        {
            var limiterentaquinta = entidades.LIMITESQUINTAs.Single(rq => rq.idlimitequinta == id);

            int inferior = Convert.ToInt32(Request.Params["Inferior"]);
            int superior = Convert.ToInt32(Request.Params["Superior"]);
            string porcentaje = Request.Params["Porcentaje"];
            int cardinal = Convert.ToInt32(Request.Params["Cardinal"]);
            bool estado = Convert.ToBoolean(Request.Params["Estado"]);
            decimal porcentajeDecimal = decimal.Parse(porcentaje.Replace(".", ","));
            try
            {
                // TODO: Add update logic here
              //  mrentaquinta.calculado = false;
              //  mrentaquinta.Devuelto = Dev;
                limiterentaquinta.inferior = inferior;
                limiterentaquinta.superior = superior;
                limiterentaquinta.porcentaje = porcentajeDecimal;//porcentaje.ToString().Length > 2 && porcentaje.ToString() != "100" ? porcentaje / 100 : porcentaje;
                limiterentaquinta.cardinal = cardinal;
                limiterentaquinta.estado = estado;
                TryUpdateModel(limiterentaquinta);
                entidades.SaveChanges();
                //entidades.RCALCULORETENCION(mrentaquinta.IdPersonal);
                return Content("OK");
            }
            catch
            {
                return Content("ERROR");
            }
        }


        public ActionResult Delete(int id, FormCollection collection)
        {
            string strResponse = String.Empty;
            var limitedelete = entidades.LIMITESQUINTAs.Single(e => e.idlimitequinta == id);

            try
            {
                if (limitedelete != null)
                {
                    entidades.LIMITESQUINTAs.DeleteObject(limitedelete);
                    entidades.SaveChanges();
                    return Content("OK");
                }
                return Content("Error");
            }
            catch (Exception e)
            {
                strResponse = "{success: false,msg:'" + e.Message + "'}";
                return Content(strResponse);
            }
        }

    }
}
